Ext.define('cp.controller.Secretaria',{
    extend: 'Ext.app.Controller',
    
    views: [
        'ui.ToolbarSecretaria',
        'ui.Statusbar',
        'ui.AlertOk',
        'ui.AlertErr',
        'ui.AlertWar',
        'ui.UndeleteConfirm',
        'ui.DeleteConfirm',
        'ui.DecoratorGrid',
        'ui.DecoratorForm',
        'ui.DecoratorFormSimple',
        'grid.GestionBase',
        'grid.GestionPaciente',
        'grid.GestionOs',
        'grid.GestionMedico',
        'form.PacienteBase',
        'form.AltaPaciente',
        'form.EditPaciente',
        'form.VerPaciente',
        'form.AltaOs',
        'form.EditOs',
        'form.VerOs',
        'form.AltaMedico',
        'form.EditMedico',
        'form.VerMedico',
        'form.OsBase',
        'form.MedicoBase',
        'grid.GestionEspecialidad',
        'form.EspecialidadBase',
        'form.AltaEspecialidad',
        'form.EditEspecialidad',
        'form.VerEspecialidad',
        'form.TurnoBase',
        'grid.GestionTurno',
        'form.AltaTurno',
        'form.VerTurno',
        'form.BuscarTurno',
        'ui.DecoratorFormSearch',
        'form.BuscarPaciente',
        'form.BuscarOs',
        'form.BuscarEspecialidad',
        'form.BuscarMedico',
        'grid.GestionLicencia',
        'form.LicenciaBase',
        'form.AltaLicencia',
        'form.BuscarLicencia'
    ],
    
    model: [
        'grid.Paciente',
        'form.Paciente',
        'grid.Os',
        'form.Os',
        'grid.Medico',
        'grid.Especialidad',
        'form.Especialidad',
        'form.Medico',
        'form.MedicoEspecialidad',
        'form.MedicoObraSocial',
        'form.MedicoDiaAtencion',
        'form.Paciente',
        'form.PacienteActivo',
        'form.ObraActiva',
        'grid.Turno',
        'form.HoraMedico',
        'form.VerTurno',
        'form.BuscarTurno',
        'grid.Licencia',
        'form.TipoLicencia'
        
        
      
       
    ], 
    store: [
        'grid.Paciente',
        'form.Paciente',
        'grid.Os',
        'form.Os',
        'grid.Medico',
        'grid.Especialidad',
        'form.Medico',
        'form.Especialidad',
        'form.MedicoEspecialidad',
        'form.MedicoObraSocial',
        'form.MedicoDiaAtencion',
        'form.PacienteActivo',
        'form.ObraActiva',
        'grid.Turno',
        'form.MedicoActivoObraSocial',
        'form.HorasMedico',
        'form.VerTurno',
        'form.MedicoActivo',
        'grid.TurnosFiltrados',
        'grid.PacientesFiltrados',
        'grid.OsFiltradas',
        'grid.EspecialidadesFiltradas',
        'grid.MedicosFiltrados',
        'form.EspecialidadActiva',
        'grid.Licencia',
        'form.TipoLicencia',
        'grid.LicenciasFiltradas'
    ],

    
    init: function(){
        this.control({
            'toolbarSecretaria button[action=sesion] menuitem[action=logout]':{
                click: function(){
                    window.location = 'bin/session/logout.php'
                }
            },
            
            'decoratorFormSearch button[action=find]':{
                click: function(btn){
                    btn.up('decoratorFormSearch').submitForm();
                }
            },
            'decoratorFormSearch button[action=clear]':{
                click: function(btn){
                    btn.up('decoratorFormSearch').clearForm();
                }
            },
            
            'undeleteConfirm button[text=Si]':{
                click: function(btn){
                    var grid = btn.up('undeleteConfirm').target;
                    var id = grid.filaSeleccionada().get(grid.idProperty);
                    grid.recuperarItemPorId(id);
                }
            },

            'deleteConfirm button[text=Si]':{
                click: function(btn){
                    var grid = btn.up('deleteConfirm').target;
                    var id = grid.filaSeleccionada().get(grid.idProperty);
                    grid.borrarItemPorId(id);
                }
            },
            
            
            /*
             * Handlers para operaciones sobre pacientes
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=pacientes]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Paciente',{});
                    var grid = Ext.create('cp.view.grid.GestionPaciente',{
                        dataStore: store
                    });
                    console.log('Grid creada, ahora, creando decorador...');
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de pacientes',
                        aGrid: grid
                    });
                    console.log('Va a mostrar...')
                    win.show();
                }
            },
            'gestionPaciente button[action=add]':{
                click: function(btn){
                    btn.up('gestionPaciente').verFormularioAlta();
                }
            },
            'gestionPaciente button[action=delete]':{
                click: function(btn){
                    btn.up('gestionPaciente').borrarItem();
                }
            },
            'gestionPaciente button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionPaciente').recuperarItem();
                }
            },
            'gestionPaciente button[action=edit]':{
                click: function(btn){
                    btn.up('gestionPaciente').verFormularioEdicion();
                }
            },
            'gestionPaciente button[action=show]':{
                click: function(btn){
                    btn.up('gestionPaciente').verFormularioVista();
                }
            },
            'decoratorForm button[action=save]':{
                click: function(btn){
                    btn.up('decoratorForm').submitForm();
                }
            },
            'decoratorForm button[action=cancel]':{
                click: function(btn){
                    btn.up('decoratorForm').cancelar();
                }
            },
            'gestionPaciente button[action=search]':{
                click: function(btn){
                    btn.up('gestionPaciente').verFormularioBusqueda();
                }
            },
                    

            /*
             * Handlers para operaciones sobre Os
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=os]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Os',{});
                    var grid = Ext.create('cp.view.grid.GestionOs',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de obras sociales',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionOs button[action=add]':{
                click: function(btn){
                    btn.up('gestionOs').verFormularioAlta();
                }
            },
            'gestionOs button[action=edit]':{
                click: function(btn){
                    btn.up('gestionOs').verFormularioEdicion();
                }
            },
            'gestionOs button[action=show]':{
                click: function(btn){
                    btn.up('gestionOs').verFormularioVista();
                }
            },
            'gestionOs button[action=delete]':{
                click: function(btn){
                    btn.up('gestionOs').borrarItem();
                }
            },
            'gestionOs button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionOs').recuperarItem();
                }
            },
            'gestionOs button[action=search]':{
                click: function(btn){
                    btn.up('gestionOs').verFormularioBusqueda();
                }
            },
                    


            /*
             * Handlers para operaciones sobre medicos
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=medico]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Medico',{});
                    var grid = Ext.create('cp.view.grid.GestionMedico',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de medicos',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionMedico button[action=add]':{
                click: function(btn){
                    btn.up('gestionMedico').verFormularioAlta();
                }
            },
            'gestionMedico button[action=delete]':{
                click: function(btn){
                    btn.up('gestionMedico').borrarItem();
                }
            },
            'gestionMedico button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionMedico').recuperarItem();
                }
            },
            'gestionMedico button[action=edit]':{
                click: function(btn){
                    btn.up('gestionMedico').verFormularioEdicion();
                }
            },
            'gestionMedico button[action=show]':{
                click: function(btn){
                    btn.up('gestionMedico').verFormularioVista();
                }
            },
            'gestionMedico button[action=search]':{
                click: function(btn){
                    btn.up('gestionMedico').verFormularioBusqueda();
                }
            },
            /*
             * Handlers para operaciones sobre especialidades
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=especialidad]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Especialidad',{});
                    var grid = Ext.create('cp.view.grid.GestionEspecialidad',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de especialidades',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionEspecialidad button[action=add]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').verFormularioAlta();
                }
            },
            'gestionEspecialidad button[action=delete]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').borrarItem();
                }
            },
            'gestionEspecialidad button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').recuperarItem();
                }
            },
            'gestionEspecialidad button[action=edit]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').verFormularioEdicion();
                }
            },
            'gestionEspecialidad button[action=show]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').verFormularioVista();
                }
            },
            'gestionEspecialidad button[action=search]':{
                click: function(btn){
                    btn.up('gestionEspecialidad').verFormularioBusqueda();
                }
            },
            /*
             * Handlers para operaciones sobre turnos
             * 
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=turno]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Turno',{});
                    var grid = Ext.create('cp.view.grid.GestionTurno',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de turnos',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionTurno button[action=add]':{
                click: function(btn){
                    btn.up('gestionTurno').verFormularioAlta();
                }
            },
            'gestionTurno button[action=show]':{
                click: function(btn){
                    btn.up('gestionTurno').verFormularioVista();
                }
            },
            'altaTurno combo[name=obra]':{
                change: function(combo){
                    var form = combo.up('altaTurno');
                    form.activarNroAfiliado(combo.getRawValue());
                }
            },
            'altaTurno datefield[name=fecha]':{
                select: function(df,value){
                    var form = df.up('altaTurno');
                    var obra = form.getForm().findField('obra').getSubmitValue();
                    form.activarNroAfiliado(obra,value);
                    form.activarMedicos(obra,value);
                }
            },
            'altaTurno combo[name=medico]':{
                change: function(combo){
                    var form = combo.up('altaTurno');
                    form.cargarHoras(form.getForm().findField('fecha').getSubmitValue());
                }
            },
            'gestionTurno button[action=delete]':{
                click: function(btn){
                    btn.up('gestionTurno').borrarItem();
                }
            },
            'gestionTurno button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionTurno').recuperarItem();
                }
            },
            'gestionTurno button[action=search]':{
                click: function(btn){
                    btn.up('gestionTurno').verFormularioBusqueda();
                }
            },
            'gestionTurno button[action=print]':{
                click: function(btn){
                    btn.up('gestionTurno').generarComprobante();
                }
            },
            /*
             * Handlers para operaciones sobre licencias
             * 
             */
            'toolbarSecretaria button[action=gestion] menuitem[action=licencia]':{
                click: function(){
                    var store = Ext.create('cp.store.grid.Licencia',{});
                    var grid = Ext.create('cp.view.grid.GestionLicencia',{
                        dataStore: store
                    });
                    var win = Ext.create('cp.view.ui.DecoratorGrid',{
                        aTitle: 'Gestión de licencia',
                        aGrid: grid
                    });
                    win.show();
                }
            },
            'gestionLicencia button[action=add]':{
                click: function(btn){
                    btn.up('gestionLicencia').verFormularioAlta();
                }
            },
            'gestionLicencia button[action=undelete]':{
                click: function(btn){
                    btn.up('gestionLicencia').recuperarItem();
                }
            },
            'gestionLicencia button[action=edit]':{
                click: function(btn){
                    btn.up('gestionLicencia').verFormularioEdicion();
                }
            },
            'gestionLicencia button[action=delete]':{
                click: function(btn){
                    btn.up('gestionLicencia').borrarItem();
                }
            },
            'gestionLicencia button[action=search]':{
                click: function(btn){
                    btn.up('gestionLicencia').verFormularioBusqueda();
                }
            }

        });
    }
});